using FluentValidation;

namespace Basket.Api.Features.Baskets.Upsert;

internal sealed class UpsertBasketCommandValidator : AbstractValidator<UpsertBasketCommand>
{
    public UpsertBasketCommandValidator()
    {
        RuleFor(x => x.Items)
            .NotEmpty();

        RuleForEach(x => x.Items).SetValidator(new BasketItemValidator());
    }

    private sealed class BasketItemValidator : AbstractValidator<UpsertBasketItemRequest>
    {
        public BasketItemValidator()
        {
            RuleFor(x => x.ProductId).NotEmpty();
            RuleFor(x => x.ProductName).NotEmpty().MaximumLength(256);
            RuleFor(x => x.UnitPrice).GreaterThanOrEqualTo(0);
            RuleFor(x => x.Quantity).GreaterThan(0);
        }
    }
}
